package system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import com.solr.web.model.vo.search.PublishtimeVo;

public class test2 {

	
	@Test
	public void searchCount(){
		String q="汽车";
		String fq="servicetype:(0 OR 2)";
		Map<String, Object> map =null;
		try {
			
		SolrDocumentList results = solrPublic_(q, fq);
		
		//索引结果总数
		long totalRecords = results.getNumFound();
		
		map = new LinkedHashMap<>();
		//有多少个统计组
		int count =0;
		//计数
		int i=0;
		//放入set去重
		Set<String> hs = new HashSet<String>();
		for (SolrDocument solrDocument : results) {
//			if (solrDocument.get("publishtime") != null && solrDocument.get("publishtime").toString().length() > 0) {
				
			String publishtime = solrDocument.get("publishtime").toString();
			System.out.println("publishtime;;;;;;"+publishtime);
//			String substring = publishtime.substring(0, 10);
			String substring = publishtime.split(" ")[0];
			hs.add(substring);
//			}
		}
		
		//根据时间排序
		List<String> list = new ArrayList<String>(hs);
		Collections.sort(list);
		
		List<PublishtimeVo> lkList = new LinkedList<PublishtimeVo>();
		PublishtimeVo vo;
		for (String publishtime : list) {
			vo = new PublishtimeVo();
			i=0;
			count++;
			for (int k = 0; k < results.size(); k++) {
//				if (results.get(k).get("publishtime") != null && results.get(k).get("publishtime").toString().length() > 0) {
					String publishtime2 = results.get(k).get("publishtime").toString();
					if(publishtime2.contains(publishtime)){
						i++;
					}
//				}
			}
			//map.put(publishtime,i);
			//System.out.println(publishtime+"-----------------------"+i);
			vo.setTime(publishtime);
			vo.setCount(i);
			lkList.add(vo);
		}
		
		for (PublishtimeVo publishtimeVo : lkList) {
			System.out.println(publishtimeVo.getTime());
		}
		
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public SolrDocumentList solrPublic_(String q,String fq){
		SolrServer solrServer = new HttpSolrServer("http://101.200.192.185:8080/solr");
		SolrDocumentList results = null;
		try {
			//查询对象
			SolrQuery query = new SolrQuery();

			StringBuffer sb = new StringBuffer();
			//设置查询关键字
			if (StringUtils.isNotEmpty(q)) {
				if (q.indexOf(" ")!=-1) {
					String[] strings = q.split(" ");
					for (int i = 0; i < strings.length; i++) {
						q='"'+strings[i]+'"';
						sb.append(q+" ");
					}
					query.setQuery(sb.toString());
				}else {
					query.setQuery('"'+q+'"');
				}
			}else {
				query.setQuery("*:*");
			}
			
			//设置默认域
			query.set("df", "title");
			query.set("qt", "/select");		
			query.set("wt", "json");
			query.setFilterQueries(fq);
			query.setRows((int) solrServer.query(query).getResults().getNumFound());
			//搜索
			QueryResponse response = solrServer.query(query);
			results = response.getResults();
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
}
